// pages/room_center/room_center.js
const db = wx.cloud.database();
const _ = db.command
const app = getApp()
let user_data = {}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_disable: '',
    room: {},
    isok: false,
    is_end: '',
    show_message:'',
    is_start:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
    // this.setData({
    //       room = app.globalData.room
    //     }) 
    //     console.log(this.data.room);
    console.log(app.globalData.room);
    user_data = app.globalData.user_data
    let room = app.globalData.room
    // room.room_name = "43242344"
    // room.room_password = "34s"
    // room._id = "17e3426e61dd2f0004dbf20d2b5f1178"
    // if (room.users == undefined) {
    //   room.users = {}
    // }
    // if (room.ready_num == undefined) {
    //   room.ready_num = 0
    // }
    console.log(room);
    this.setData({
      room
    })
  this.get_watch()
  },
  get_watch() {
    let room = this.data.room
    let that = this
    let isok = 0
    
    let is_start = this.data.is_start
    console.log("1111111",room);
    const watcher = db.collection('room').doc(room._id).watch({
      onChange: (snapshot) => {
        console.log(snapshot);
        room = snapshot.docs[0]
        let temp = Object.keys(room.users)
        let temp1 = snapshot.docChanges[0].updateFields
        if (temp.length > 1) {
          isok = true
          is_start = true
        }
        if (room.users != undefined) {
          if (room.users[user_data.color] != undefined) {
            if (room.users[user_data.color].ready == true) {
              is_start = false
            }
          }
            
        }
        that.setData({
          room,
          isok,
          is_start
        })
        // if (room.is_start != undefined &&  room.is_start) {
        //   wx.navigateTo({
        //     url: '/pages/game_center/game_center',
        //   });
        // }
        console.log("updateTime:",room._updateTime);
        let cnt = room.ready_num
        let cnt1 = 0
        for (let i in room.users) {
          if(i != undefined) cnt1++
        }
        if (cnt == cnt1 &&  cnt != 0) {
          if (room.mode == 1) {
            wx.navigateTo({
              url: '/pages/game_center/game_center',
            });
          }
        }
      },
      onError:(err=>{console.log(err);})
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
     
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },
  choice_temp(e) {
    let that = this
    console.log(e.target.dataset);
    let choice_color = e.target.dataset.target
    let room = this.data.room
    user_data.color = choice_color
    user_data.tasks = room.tasks
    user_data.tasks_num = 0
    user_data.ready = false
    room.users[choice_color] = user_data
    app.globalData.user_data = user_data
    console.log("choice",room);
    this.setData({
        room
    })
    wx.cloud.callFunction({
      // 需调用的云函数名
      name: 'updata_room',
      // 传给云函数的参数
      data: {
        users: room.users,
        _id:room._id
      },
      // 成功回调
      complete: res => {
        console.log(res)
      }
    })
    
  },
  async start_game() {
    let room = this.data.room
    app.globalData.room = room
    room.users[user_data.color].ready = true
    wx.cloud.callFunction({
      // 需调用的云函数名
      name: 'update_ready_num',
      // 传给云函数的参数
      data: {
        _id: room._id,
        users:room.users
      },
      // 成功回调
      complete: res => {
        console.log(res)
      }
    })
    if(room.mode == 1){
      let room = this.data.room
      wx.cloud.callFunction({
        name: 'update_time_state',  // 云函数名称
        data: {  // 传给云函数的参数
          _id: room._id,
          is_time: true,
          users:room.users
        },
        complete: res => {
          // this.setData({
          //   start_time:true,
          // })
        }
      })
      // wx.navigateTo({
      //   url: '/pages/game_center/game_center'
      // });
    }else{
      wx.navigateTo({
        url: '/pages/time/time'
      });
    }
    this.setData({
      is_start:false
    })
  }
 
})

